package com.lysj.bhp.system.base;

import com.lysj.bhp.manager.domain.Staff;
import com.lysj.bhp.system.service.MailService;
import com.lysj.bhp.user.domain.User;
import com.lysj.bhp.util.web.Resp;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.method.support.HandlerMethodReturnValueHandler;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 基础的容器,此容器包含所有Service和Repository的对象(单例)
 */
@Component
public class RootContent extends WebMvcConfigurerAdapter {
	// =============公共常量======================
	public static final String DOMAIN_NAME = "https://www.bhpcash.io:8085/";
//	public static final String DOMAIN_NAME = "drj8565402.55555.io:15257/";
//	public static final String DOMAIN_NAME = "http://drj8565402.55555.io:15257/";
	private final static Logger logger = LoggerFactory.getLogger(RootContent.class);

	// =============== 公共函数==========================

	@ExceptionHandler(Exception.class)
	public Resp handleException(Exception e) {
		e.printStackTrace();
		StackTraceElement[] stackTraceElements = e.getStackTrace();
		logger.error(stackTraceElements[0]+"--"+e.toString());
		return new Resp(Resp.INNER_ERROR, "System is not accessible now, please try again!");
	}

	/**
	 * 获取目前登录的员工
	 */
	protected Staff currentStaff() {
		return (Staff) request.getAttribute("staff");
	}

	/**
	 * 获取目前登录的用户
	 */
	protected User currentUser() {
		return (User) request.getAttribute("user");
	}


	@InitBinder
	public void InitBinder(WebDataBinder binder) {
		binder.registerCustomEditor(Date.class, null, new CustomDateEditor(new SimpleDateFormat("yyyy-MM-dd"), true));
		binder.registerCustomEditor(Date.class, null, new CustomDateEditor(new SimpleDateFormat("yyyy-MM-dd hh:mm"), true));
	}
	

	@Resource
	protected HttpServletRequest request;
	@Resource
	protected HttpServletResponse response;
	@Resource
	protected MailService mailService;


}
